/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.report.html.provider;

import org.open.cobertura.data.ICoberturaLineCovered;
import org.open.cobertura.model.LineCoverage;
import org.open.cobertura.report.html.ISourceHighLight;

import java.util.List;

public class SourceHighLight implements ISourceHighLight {
    private final String lineStyle="<span class='%s'>%s</span>";

    @Override
    public void highLight(List<LineCoverage> lineCoveredList, List<String> sourceList) {
        for(int i=0;i<lineCoveredList.size();i++){
            LineCoverage coberturaLineCovered=lineCoveredList.get(i);
            int index=coberturaLineCovered.getLine()==0?0:coberturaLineCovered.getLine()-1;
            String line=sourceList.get(index);
            int branchTotal=coberturaLineCovered.getBranchesTotalCount();
            int branchCoveraged=coberturaLineCovered.getBranchesCoveredCount();
            int instructionCoveraged=coberturaLineCovered.getInstruCoveredCount();
            int instructionTotal=coberturaLineCovered.getInstruTotalCount();
            int status=0;
            if (instructionTotal>0 && (instructionCoveraged==instructionTotal) &
                    (branchCoveraged==branchTotal)){
                status=3;
            }else if(instructionTotal>0 && (instructionCoveraged>0 &
                    instructionCoveraged<instructionTotal) ||
                    (branchCoveraged>0 && branchCoveraged<branchTotal)){
                status=2;
            }else if(instructionTotal>0 && instructionCoveraged==0){
                status=1;
            }
            String formatLine=format(line,status);
            sourceList.set(index,formatLine);
        }
    }

    /**
     * 格式化源代码
     * @param line 行
     * @param status 覆盖状态
     */
    private String format(String line,int status){
        String cssName="no";
        switch (status){
            case 0:
                break;
            case 1:
                cssName="nc";
                break;
            case 2:
                cssName="pc";
                break;
            case 3:
                cssName="ac";
                break;
        }
        return String.format(lineStyle,cssName,line);
    }
}
